package com.mac.cxcybackground.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mac.cxcybackground.bean.StudentDept;
import com.mac.cxcybackground.bean.Students;
import com.mac.cxcybackground.mapper.DeptsMapper;
import com.mac.cxcybackground.mapper.StudentDeptMapper;
import com.mac.cxcybackground.mapper.StudentsMapper;
import com.mac.cxcybackground.service.DeptsService;
import com.mac.cxcybackground.service.StudentsService;
import com.mac.cxcybackground.vo.StudentsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

@Controller
public class StudentsDept05Controller {
    @Autowired
    StudentDeptMapper studentDeptMapper;
    @Autowired
    StudentsMapper studentsMapper;
    @Autowired
    StudentsService studentsService;
    @Autowired
    DeptsService deptsService;
    @Autowired
    DeptsMapper deptsMapper;
    @GetMapping("/student_dept_05_table")
    public String StudentFind(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                              Model model){
        IPage<Students> studentsIPage = new Page<>(pn,8);
        QueryWrapper w = new QueryWrapper();
        w.eq("did",5L);
        IPage<Students> page = studentsService.page(studentsIPage, w);
        List<Students> studentsList =page.getRecords();
        List<StudentsVo> studentsVoList = new ArrayList<>();
        for (Students students : studentsList) {
            StudentsVo studentsVo =new StudentsVo();
            BeanUtils.copyProperties(students,studentsVo);
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("did",students.getDId());
            studentsVo.setDName( studentDeptMapper.selectOne(wrapper).getDName());
            studentsVoList.add(studentsVo);
        }
        model.addAttribute("page",page);
        model.addAttribute("result",studentsVoList);
        return "table/student_dept_05_table";
    }
    @GetMapping("/student_dept_05/delete/{id}")
    public String Student_dept_05Delete(@PathVariable("id") String sNo,
                                        @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                                        RedirectAttributes ra){
        studentsService.removeById(sNo);
        ra.addAttribute("pn",pn);
        return "redirect:/student_dept_05_table";
    }
    @GetMapping("student_dept_05_add_table")
    public String student_dept_05_add_table(Model model){
        //查出所有部门信息
        List<StudentDept> deptsList = studentDeptMapper.selectList(null);
        model.addAttribute("deptsList",deptsList);
        return "table/student_dept_05_add_table";
    }
    @PostMapping("student_dept_05/add")
    public String Student_dept_05Add(Students students){
        Students student = new Students(students.getSNo(),students.getSName(),students.getSex(),
                students.getSGrade(),students.getSProfessional(),students.getQq(),students.getTelephoneNum(),
                students.getDId());
        studentsService.save(student);
        return "redirect:/student_dept_05_table";
    }
    @GetMapping("/student_dept_05/update-get/{id}")
    public String Student_dept_05Update_get(@PathVariable("id") String sNo,
                                            @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                            RedirectAttributes ra,Model model){
        Students result = studentsService.getById(sNo);
        ra.addAttribute("pn",pn);
        model.addAttribute("result",result);
        //查出所有部门信息
        List<StudentDept> deptsList = studentDeptMapper.selectList(null);
        model.addAttribute("deptsList",deptsList);
        model.addAttribute("deptsList",deptsList);
        return "table/student_dept_05_update_table";
    }
    @GetMapping("/student_dept_05/update/{id}")
    public String Student_dept_05Update(@PathVariable("id") String sno,
                                        Students students){
      Students student = new Students(sno,students.getSName(),students.getSex(),students.getSGrade(),
              students.getSProfessional(),students.getQq(),students.getTelephoneNum(),students.getDId());
      studentsService.updateById(student);
      return "redirect:/student_dept_05_table";
    }
}